package com.mxwl.cbg.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.mxwl.cbg.common.config.GameConfig;
import lombok.Data;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {
    static Map<String, Object> gameConfig = GameConfig.getGame_config();

    static List<List<Object>> weaponArmors = (List<List<Object>>) gameConfig.get("weapon_armors");
    static List<Map<String, Object>> wjdEquipKindid = (List<Map<String, Object>>) gameConfig.get("wjd_equip_kindid");
    static Map<String, Map<String, String>> equipInfo = (Map<String, Map<String, String>>) gameConfig.get("equip_info");

    public static void main(String[] args) throws Exception {
        try (InputStream inputStream = GameConfig.class.getClassLoader().getResourceAsStream("equipment.json")) {
            if (inputStream == null) {
                throw new RuntimeException("Unable to find game_config.json in classpath.");
            }
            JsonMapper jsonMapper = new JsonMapper();
            List<Map<String, Object>> xx = jsonMapper.readValue(inputStream, List.class);


            //存储最终的 数据
            Map<Integer, EquipmentDesc> equipmentDescMap = new HashMap<>();
            ArrayList<EquipmentDesc> equipmentDescArrayList = new ArrayList<>();
            for (Map<String, Object> equip : xx) {
                EquipmentDesc equipmentDesc = new EquipmentDesc();
                equipmentDesc.setName(equip.get("name").toString());
                equipmentDesc.setMin_level(Integer.parseInt(equip.get("min_level").toString()));
                equipmentDesc.setMax_level(Integer.parseInt(equip.get("max_level").toString()));
                equipmentDesc.setReinforced(Boolean.parseBoolean(equip.get("reinforced").toString()));
                equipmentDesc.setType(String.valueOf(equip.get("type")));
                equipmentDesc.setKind_name(equip.get("kind").toString());
                //先尝试从所有装备中查找kindid
                int kindid = getEquipmentKindid(equipmentDesc.getType());
                if (kindid == -1) {
                    kindid = getEquipmentKindidFromWeaponArmors(equipmentDesc.getType());
                }
                if (kindid == -1) {
                    System.out.println("未找到装备的kind_id：" + equip.get("name"));
                    continue;
                }
                equipmentDesc.setKind_id(kindid);


                EquipmentDesc equipmentDesc2 = null;
                if (equipmentDesc.getName().contains("（乾坤）")) {
                    equipmentDesc2 = deepCopy(equipmentDesc, EquipmentDesc.class);
                    //一个装备的名称后面是（乾）,一个是（坤）
                    equipmentDesc.setName(equip.get("name").toString().replace("（乾坤）", "（乾）"));
                    equipmentDesc2.setName(equip.get("name").toString().replace("（乾坤）", "（坤）"));
                }

                for (String id : equipInfo.keySet()) {
                    if (equipmentDesc.getName().equals("月光")){
                        if (Objects.equals(equipmentDesc.getType(), "双剑")){
                            id = "2151";
                            Map<String, String> map = equipInfo.get(id);
                            equipmentDesc.setDesc(map.get("desc"));
                            equipmentDesc.setId(Integer.parseInt(id));
                            break;
                        }else if (Objects.equals(equipmentDesc.getType(), "双环")){
                            id = "2052";
                            Map<String, String> map = equipInfo.get(id);
                            equipmentDesc.setDesc(map.get("desc"));
                            equipmentDesc.setId(Integer.parseInt(id));
                            break;
                        }
                    }
                    Map<String, String> map = equipInfo.get(id);
                    if (equipmentDesc.getName().equals(map.get("name"))) {
                        equipmentDesc.setDesc(map.get("desc"));
                        equipmentDesc.setId(Integer.parseInt(id));
                    }
                    if (equipmentDesc2 != null) {
                        if (equipmentDesc2.getName().equals(map.get("name"))) {
                            equipmentDesc2.setDesc(map.get("desc"));
                            equipmentDesc2.setId(Integer.parseInt(id));
                        }
                    }
                }

                equipmentDescMap.put(equipmentDesc.getId(), equipmentDesc);
                equipmentDescArrayList.add(equipmentDesc);
                if (equipmentDesc2 != null) {
                    equipmentDescMap.put(equipmentDesc2.getId(), equipmentDesc2);
                    equipmentDescArrayList.add(equipmentDesc2);
                }
            }

            equipmentDescArrayList.stream().forEach(equipmentDesc -> {
                if (equipmentDesc.getId() == null) {
                    System.out.println("未找到装备id：" + equipmentDesc.getName());
                }
            });
            //将写为json文件
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, EquipmentDesc> safeMap = new HashMap<>();
            for (Map.Entry<Integer, EquipmentDesc> entry : equipmentDescMap.entrySet()) {
                if (entry.getKey() != null) {
                    safeMap.put(String.valueOf(entry.getKey()), entry.getValue());
                }
            }
            objectMapper.writeValue(new java.io.File("equipment_config.json"), safeMap);
        } catch (IOException e) {
            throw new RuntimeException("Failed to load configuration", e);
        }

    }

    @Data
    public static class EquipmentDesc {
        private Integer id;
        private String name;
        private String kind_name;
        private Integer kind_id;
        private Integer min_level;
        private Integer max_level;
        private Boolean reinforced;
        private String type;
        private String desc;
    }

    private static int getEquipmentKindidFromWeaponArmors(String name) {
        for (List<Object> weaponArmor : weaponArmors) {
            if (name.equals(weaponArmor.get(1))) {
                return Integer.parseInt(weaponArmor.get(0).toString());
            }
        }
        return -1;
    }

    private static int getEquipmentKindid(String name) {
        for (Map<String, Object> map : wjdEquipKindid) {
            if (name.equals(map.get("label"))) {
                return Integer.parseInt(map.get("value").toString());
            }
        }
        return -1;
    }

    private static final ObjectMapper mapper = new ObjectMapper();

    public static <T> T deepCopy(T source, Class<T> clazz) throws Exception {
        return mapper.readValue(mapper.writeValueAsBytes(source), clazz);
    }
}